import { Component, OnInit } from '@angular/core';
import {DeviceService} from '../../../svc/device.svc';
import {MessageService} from 'primeng/api';

@Component({
  selector: 'app-data-communication',
  templateUrl: './data-communication.component.html',
  styleUrls: ['./data-communication.component.css']
})
export class DataCommunicationComponent implements OnInit {
    dataCommunicationInformation = {
        url: '',
        userName: '',
        passWord: '',
        networkId: '',
        nodeId: '',
        reportInterval: '',
        resetMultiplier: '',
    };
    communicationMode = [
        {mode: 'Local'},
        {mode: 'Cloud'}
    ];
    selectedCommunicationMode = {mode: 'Cloud'};
    intervalUnit = [
        {unit: 'Second(s)'},
        {unit: 'Minute(s)'}
    ];
    selectIntervalUnit;
    constructor(private ds: DeviceService, private ms: MessageService) { }

    ngOnInit(): void {
        this.getDataCommunicationSettings();
    }

    getDataCommunicationSettings(): void {
        this.ds.getDataCommunicationSettings().subscribe(data => {
            if (data && data.hasOwnProperty('url')) {
                this.dataCommunicationInformation.url = data.url;
            }
        });
    }

    setUdp(): void {
        const udpTarget = this.dataCommunicationInformation.url;
        if(!this.ds.isValidIP(udpTarget)) {
            // this.ds.msgs.push({severity: 'error', summary: 'Error', detail: 'Invalid IP Format'});
            this.ms.add({severity: 'error', summary: 'Error', detail: 'Invalid IP Format', life: 1000});
            return;
        }

        this.ds.setUdpTarget(udpTarget).subscribe(res => {
            console.log(res);
            this.ms.add({severity: 'success', summary: 'Success', life: 1000});
        });
    }
}
